package cn.gzsf.springboot05.controller;

import cn.gzsf.springboot05.entity.User;
import cn.gzsf.springboot05.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/reg")
    public String reg(User user){
        System.out.println(user);
        User u = userMapper.selectByUsername(user.getUsername());
        if(u==null){//没有查到用户名相同的用户
            userMapper.insert(user);
            return "注册成功";
        }
        //查到了，返回注册失败
        return "用户名已被占用，注册失败！<a href='/reg.html'>回去注册</a>";
    }

    @RequestMapping("/login")
    public String login(User user){
        User u = userMapper.selectByUsername(user.getUsername());
        if(u==null){//登录，查无此人
            return "登录失败,<a href='/reg.html'>去注册</a>";
        }
        //程序走到这里，说明查到了
        if(u.getPassword().equals(user.getPassword())){//对比用户输入密码和数据库中的密码
            return "登录成功";
        }
        //程序走到这里，说明密码不对
        return "登录失败";
    }
}
